<template>
  <div id="app">
    <!-- 管理员模式 - 显示完整导航 -->
    <template v-if="!isCustomerMode">
      <div class="app-header">
        <el-menu
            :default-active="$route.path"
            class="app-menu"
            mode="horizontal"
            router
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
          <div class="logo">
            <span class="logo-text">📕 数据搜索系统</span>
          </div>
          <el-menu-item index="/search">
            <el-icon><Search /></el-icon>
            机票搜索
          </el-menu-item>
          <el-menu-item index="/hotel-search">
            <el-icon><Search /></el-icon>
            酒店搜索
          </el-menu-item>
          <el-menu-item index="/attractions">
            <el-icon><Search /></el-icon>
            景点搜索
          </el-menu-item>
          <el-menu-item index="/product-management">
            <el-icon><ShoppingBag /></el-icon>
            商品路线管理
          </el-menu-item>
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            系统设置
          </el-menu-item>
        </el-menu>
      </div>

      <div class="app-main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>

      <div class="app-footer">
        <p>&copy; 2025 数据搜索系统. All rights reserved.</p>
      </div>
    </template>

    <!-- 客户模式 - 简化界面 -->
    <template v-else>
      <div class="customer-main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>

      <div class="customer-footer">
        <p>内容编辑系统 &copy; 2025</p>
      </div>
    </template>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { Search, Setting, ShoppingBag } from '@element-plus/icons-vue'
import { useSystemStore } from '@/stores/system'
import { useProductStore } from '@/stores/product'
import { ElMessage } from 'element-plus'

const route = useRoute()
const systemStore = useSystemStore()
const productStore = useProductStore()

// 判断是否为客户模式
const isCustomerMode = computed(() => {
  return route.query.standalone === 'true' ||
      route.query.mode === 'customer' ||
      route.path.includes('/customer/') ||
      window.location.search.includes('standalone=true')
})

// 当前产品信息
const currentProduct = computed(() => productStore.currentProduct)

// 检查系统状态
onMounted(async () => {
  // 只在管理员模式下检查系统状态
  if (!isCustomerMode.value) {
    try {
      await systemStore.checkSystemStatus()
    } catch (error) {
      console.warn('无法连接到后端服务')
      ElMessage.warning('后端服务连接异常，部分功能可能受限')
    }
  }

  // 如果是客户模式且URL中有产品ID，获取产品信息
  if (isCustomerMode.value && route.params.id) {
    try {
      await productStore.getProductById(route.params.id)
    } catch (error) {
      console.error('获取产品信息失败:', error)
    }
  }
})
</script>

<style>
#app {
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 管理员模式样式 */
.app-header {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
}

.app-menu {
  display: flex;
  align-items: center;
}

.logo {
  flex-shrink: 0;
  padding-right: 20px;
}

.logo-text {
  font-size: 20px;
  font-weight: bold;
  color: #fff;
}

.app-main {
  flex: 1;
  padding: 20px;
  background: #f0f2f5;
  min-height: calc(100vh - 120px);
}

.app-footer {
  background: #fff;
  text-align: center;
  padding: 15px;
  border-top: 1px solid #e4e7ed;
  color: #909399;
}

/* 客户模式样式 */
.customer-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.customer-header .logo-text {
  color: white;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.product-info {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.customer-main {
  flex: 1;
  background: #f5f5f5;
  min-height: calc(100vh - 120px);
}

.customer-footer {
  background: #fff;
  text-align: center;
  padding: 12px;
  border-top: 1px solid #e4e7ed;
  color: #909399;
  font-size: 12px;
}

/* 页面过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>